﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.News.Process;
using TjZheng.Framework;
using TjZheng.News.DataEntity;
using TjZheng.News.Business;
using TjZheng.Cache;

namespace TjZheng.News.Web.Controllers
{
    public class NewsController : BaseController
    {
        [OutputCache(Duration = 30)]
        public ActionResult Index()
        {
            BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo();
            CategoryInfo objCategoryInfo = objBizCategoryInfo.GetRecordCache("", 2, TimeSpan.FromMinutes(1));
            if (objCategoryInfo == null)
            {
                return Http404();
            }
            ViewBag.CategoryInfoList = objBizCategoryInfo.GetListCache("", TimeSpan.FromMinutes(1), "ParentId=2", "SortIndex asc");
            BizPlaceInfo objBizPlaceInfo = new BizPlaceInfo();
            ViewBag.SearchRecommend = objBizPlaceInfo.GetListCache("", TimeSpan.FromMinutes(1), "PlaceConfigID=2", "PublishDate desc", "top 1 *").FirstOrDefault();
            return View(objCategoryInfo);
        }

        [ChildActionOnly]
        public ActionResult CategoryModule(CategoryInfo objCategoryInfo)
        {
            if (objCategoryInfo == null)
            {
                return Content("<!--文章分类不存在-->");
            }
            if (!this.CurrentProcess.ExistTableArticleInfo(objCategoryInfo.CategoryId))
            {
                return Content("<!--文章表不存在-->");
            }
            ViewBag.CategoryInfo = objCategoryInfo;
            BizArticleInfo objBizArticleInfo = new BizArticleInfo(objCategoryInfo.CategoryId.ToString());
            List<ArticleInfo> objArticleInfoList = objBizArticleInfo.GetListCache("", TimeSpan.FromMinutes(1), "", "PublishDate desc", "top 5 *");
            return View(objArticleInfoList.CreateArticleResultInfoList(false, false).ConvertTargetUrl(objCategoryInfo));
        }

        [OutputCache(Duration = 30)]
        public ActionResult ArticleList(int? id)
        {
            if (!id.HasValue || id <= 0)
            {
                return Http404();
            }
            if (!this.CurrentProcess.ExistTableArticleInfo(id.Value))
            {
                return Http404();
            }
            BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo();
            CategoryInfo objCategoryInfo = objBizCategoryInfo.GetRecordCache("", id.Value, TimeSpan.FromMinutes(1));
            if (objCategoryInfo == null)
            {
                return Http404();
            }
            ViewBag.CategoryInfoList = objBizCategoryInfo.GetListCache("", TimeSpan.FromMinutes(1), "ParentId=2", "SortIndex asc");
            BizPlaceInfo objBizPlaceInfo = new BizPlaceInfo();
            ViewBag.SearchRecommend = objBizPlaceInfo.GetListCache("", TimeSpan.FromMinutes(1), "PlaceConfigID=2", "PublishDate desc", "top 1 *").FirstOrDefault();
            return View(objCategoryInfo);
        }

        [ChildActionOnly]
        public ActionResult SlidesModule(int? placeConfigID)
        {
            if (!placeConfigID.HasValue)
            {
                return Content("<!--信息位编号为空-->");
            }
            BizPlaceInfo objBizPlaceInfo = new BizPlaceInfo();
            return View(objBizPlaceInfo.GetPageList(0, 5, "PlaceConfigID=" + placeConfigID, "PublishDate desc"));
        }

        [OutputCache(Duration = 30)]
        [Route("news/{categoryId}-{articleId}-{publishDate}")]
        public virtual ActionResult Article(int? categoryId, int? articleId, string publishDate)
        {
            if (!categoryId.HasValue || categoryId <= 0)
            {
                return Http404();
            }
            if (!articleId.HasValue || articleId <= 0)
            {
                return Http404();
            }
            if (publishDate.IsNull())
            {
                return Http404();
            }
            ArticleInfo objArticleInfo = this.CurrentProcess.GetArticleInfo(categoryId.Value, articleId.Value);
            if (objArticleInfo == null)
            {
                return Http404();
            }
            if (!string.Equals(objArticleInfo.PublishDate.ToString("yyyyMMdd"), publishDate))
            {
                return Http404();
            }
            bool isLoadPictureCollection = false;
            if (objArticleInfo.ArticleType.IsNoNull())
            {
                string[] articleTypes = objArticleInfo.ArticleType.Split(',');
                if (articleTypes.Contains("1"))
                {
                    isLoadPictureCollection = true;
                }
                //图集视图
            }
            return View(objArticleInfo.CreateArticleResultInfo(true, isLoadPictureCollection).ConvertTargetUrl());
        }


        public override ArticleInfoProcess CreateArticleInfoProcess()
        {
            return new ArticleInfoProcess();
        }
    }
}
